using System;
using System.Collections.Generic;
using System.Text;

namespace Jasongozy.Alaton {
    public static class TypeParser {
        #region StringToValueType

        public static int ToInt(string str, int defVal) {
            if (!string.IsNullOrEmpty(str)) {
                int res = 0;
                if (int.TryParse(str, out res)) {
                    return res;
                }
            }
            return defVal;
        }
        public static Int16 ToInt16(string str, Int16 defVal) {
            Int16 res = defVal;
            if (!string.IsNullOrEmpty(str)) {
                if (!Int16.TryParse(str, out res)) {
                    res = defVal;
                }
            }
            return res;
        }
        public static UInt32 ToUInt32(string str, UInt32 defVal) {
            UInt32 res = defVal;
            if (!string.IsNullOrEmpty(str)) {
                if (!UInt32.TryParse(str, out res)) {
                    res = defVal;
                }
            }
            return res;
        }
        public static UInt16 ToUInt16(string str, UInt16 defVal) {
            UInt16 res = defVal;
            if (!string.IsNullOrEmpty(str)) {
                if (!UInt16.TryParse(str, out res)) {
                    res = defVal;
                }
            }
            return res;
        }
        public static long ToLong(string str, long defVal) {
            long res = defVal;
            if (!string.IsNullOrEmpty(str)) {
                if (!long.TryParse(str, out res)) {
                    res = defVal;
                }
            }
            return res;
        }
        public static ulong ToUlong(string str, ulong defVal) {
            ulong res = defVal;
            if (!string.IsNullOrEmpty(str)) {
                if (!ulong.TryParse(str, out res)) {
                    res = defVal;
                }
            }
            return res;
        }

        public static float ToFloat(string str, float defVal) {
            float res = defVal;
            if (!string.IsNullOrEmpty(str)) {
                if (!float.TryParse(str, out res)) {
                    res = defVal;
                }
            }
            return res;
        }
        public static double ToDouble(string str, double defVal) {
            double res = defVal;
            if (!string.IsNullOrEmpty(str)) {
                if (!double.TryParse(str, out res)) {
                    res = defVal;
                }
            }
            return res;
        }

        public static DateTime ToDateTime(string str, DateTime defVal) {
            DateTime res = defVal;
            if (!string.IsNullOrEmpty(str)) {
                if (!DateTime.TryParse(str, out res)) {
                    res = defVal;
                }
            }
            return res;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="str">True/False or 1/0</param>
        /// <param name="defVal"></param>
        /// <returns></returns>
        public static bool ToBool(string str, bool defVal) {
            if (!string.IsNullOrEmpty(str)) {
                if (string.Compare(str.ToLower(), "true", true) == 0) {
                    return true;
                } else if (string.Compare(str.ToLower(), "false", true) == 0) {
                    return false;
                }
                int defInt = defVal ? 1 : 0;
                if (ToInt(str,defInt) != defInt) {
                    return ToInt(str,-1) > 0;
                }
            }
            return defVal;
        }

        public static TimeSpan ToTimeSpan(string str, TimeSpan defVal) {
            TimeSpan res = defVal;
            if (!string.IsNullOrEmpty(str)) {
                if (!TimeSpan.TryParse(str, out res)) {
                    res = defVal;
                }
            }
            return res;
        }

        #endregion
    }
}
